﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using Newtonsoft.Json;

namespace DAL
{
    public class DALCMS_Article
    {
        public List<CMS_Article> GetCMS_ArticleALL()
        {
            using (var db = new KQCMS())
            {
                return db.CMS_Article.ToList();
            }
        }
        public int InsertCMS_Article(CMS_Article CMS_Article)
        {
            using (var db = new KQCMS())
            {
                try
                {
                    db.Entry(CMS_Article).State = EntityState.Added;
                    db.SaveChanges();
                    return 0;
                }
                catch (Exception)
                {

                    return 1;
                }


            }
        }
        public int UpdateCMS_Article(CMS_Article CMS_Article)
        {
            using (var db = new KQCMS())
            {

                var xg = db.CMS_Article.SingleOrDefault();
                try
                {
                    db.Entry(xg).State = EntityState.Modified;
                    db.SaveChanges();
                    return 0;
                }
                catch (Exception)
                {

                    return 1;
                }


            }
        }
        public List<CMS_Article> GetCMS_ArticlesTj(DateTime ctime, DateTime ptime, int State)
        {
            using (var db = new KQCMS())
            {
                return db.CMS_Article.Where(x => x.ctime == ctime || x.ptime == ptime || x.state == State).ToList();
            }
        }
        public int DeleteCMS_Articles(int? id)
        {
            using (var db = new KQCMS())
            {
                    var sc = db.CMS_Article.SingleOrDefault(x => x.aid == id);
                    db.Entry(sc).State = EntityState.Deleted;
                    return  db.SaveChanges();
            }
        }

        public List<View_Article> GetCMS_ArticleAll()
        {
            using (var db = new KQCMS())
            {
                return db.View_Article.ToList();
            }
        }

        public string GetPageCMS_Articles(int pageindex, int pagesize, string aid)
        {
            var db = new KQCMS();
            var list = db.View_Article.AsQueryable();
            //所有数据
            list = list.Where(x => x.state == 1 || x.state == 2);
            if (!string.IsNullOrEmpty(aid))
            {
                int id = Convert.ToInt32(aid);
                list = list.Where(x => x.aid == id);
            }
            //某一页数据
            var data = list.OrderBy(x => x.aid)
           .Skip((pageindex - 1) * pagesize)//排除前面多少行
           .Take(pagesize)//只取前面多行
           .ToList();             //得到数据的条数

            //总行数
            int rowCount = list.ToList().Count();
            //总页数
            var pageSum = 0;
            //通过计算，得到分页应该需要分几页，其中不满一页的数据按一页计算
            if (rowCount % pagesize != 0)
            {
                pageSum = rowCount / pagesize + 1;
            }
            else
            {
                pageSum = rowCount / pagesize;
            }

            //转成Json格式    总页数：9页                  当前页码                                           当前页的数据              
            var strResult = "{\"pageSum\":" + pageSum + ",\"CurrentPage\":" + pageindex + ",\"list\":" + JsonConvert.SerializeObject(data)+ "}";
            return strResult;
        }
    }
}
